package com.myzk.example.core;

import com.alibaba.fastjson.JSON;
import com.myzk.example.consts.ResultCodeMsg;

import java.util.HashMap;
import java.util.Map;

/**
 * @author  mayong
 * @desc Result工具类
 * @date 2020年11月4日15:38:21
 */
public class ResultUtil {

    public static <T> Result<T> success(T data){
        Result<T> result = new Result<>();
        result.setCodeAndMessage(ResultCodeMsg.SUCCESS);
        result.setData(data);
        result.setSuccess(true);
        return result;
    }

    public static <T> Result<T> success(){
        return success(null);
    }

    public static Result error(String code,String message){
        Result result = new Result();
        result.setCode(code);
        result.setMessage(message);
        result.setSuccess(false);
        return result;
    }

    public static Result error(String code){
        return error(code,ResultCodeMsg.ERROR_MSG);
    }

    public static Result error(){
        return error(ResultCodeMsg.ERROR_CODE,ResultCodeMsg.ERROR_MSG);
    }

    public static <T> Result<T> newResult(String code,String message,T data) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        result.setData(data);
        return  result;
    }


    public static void main(String[] args) {
        Map data = new HashMap<>();
        data.put("status","200");
        data.put("message","ok");
        Result<Map> result = ResultUtil.newResult("200", "ok", data);
        System.out.println("result = " + result);
        System.out.println("JSON.toJSONString(result) = " + JSON.toJSONString(result));
    }


}
